// JointFormatManager.cpp: implementation of the JointFormatManager class.
//
//////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "JointFormatManager.h"
#include "ProductCatalogManager.h"
#include "EDVector3d.h"
#include "Real.h"
#include "user.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

namespace JointSystem
{

JointFormatManager* JointFormatManager::g_Instance = 0;

JointFormatManager* JointFormatManager::Instance()
{
	static JointFormatManager theManager;
	return &theManager;
}

JointFormatManager::JointFormatManager()
{

}

JointFormatManager::~JointFormatManager()
{
	UnregisterAll();
}

VOID JointFormatManager::Register( JointFormat* aJFormat )
{
	if ( !HasRegistered( aJFormat ) )
		m_JointFormats.Add( aJFormat );
}

VOID JointFormatManager::UnregisterAll()
{
	for ( INT i = 0; i < m_JointFormats.GetSize(); i++ )
		delete m_JointFormats.GetAt( i );
	m_JointFormats.RemoveAll();
}

BOOL JointFormatManager::HasRegistered( JointFormat* aJFormat )
{
	return GetJointFormat( aJFormat->MatchingJointPortFormatID(),
		aJFormat->MatchedJointPortFormatID() ) != NULL;
}

BOOL JointFormatManager::HasRegistered( CONST CString& matchingJFormat, CONST CString& matchedJFormat )
{
	return GetJointFormat( matchingJFormat, matchedJFormat ) != NULL;
}

INT JointFormatManager::GetItemIndex( CONST CString& matchingJFormat, CONST CString& matchedJFormat ) CONST
{
	JointFormat* ptr = 0;
	for ( INT i = 0; i < m_JointFormats.GetSize(); i++ )
	{
		ptr = m_JointFormats.GetAt( i );
		if ( ptr->MatchingJointPortFormatID() == matchingJFormat
			&& ptr->MatchedJointPortFormatID() == matchedJFormat )
			return i;
		ptr = 0;
	}
	return -1;
}

JointFormat* JointFormatManager::GetJointFormat( CONST CString& matchingJFormat, CONST CString& matchedJFormat )
{
	INT index = GetItemIndex( matchingJFormat, matchedJFormat );
	if ( index != -1 )
		return m_JointFormats.GetAt( index );

	_RecordsetPtr rs( __uuidof( Recordset ) );
	CString strSource;
	JointFormat* aJFormat = NULL;
	strSource.Format( _T( "SELECT * FROM JPFormat_Matching_Rule WHERE JPF_Matching=\'%s\' \
		AND JPF_Matched=\'%s\'" ), matchingJFormat, matchedJFormat );

	try
	{
		rs->Open( (_variant_t)strSource, (IUnknown*)*ProductCatalogManager::Instance()->DataSourceConnection(),
			adOpenStatic, adLockOptimistic, adCmdText );
		
		if ( rs->adoEOF && rs->BOF )
		{
			rs->Close();
			return NULL;
		}

		aJFormat = new JointFormat;
		aJFormat->MatchingJointPortFormatID( vtos( rs->GetCollect( _T( "JPF_Matching" ) ) ) );
		aJFormat->MatchedJointPortFormatID( vtos( rs->GetCollect( _T( "JPF_Matched" ) ) ) );
		CString strAffixArrayDir( vtos( rs->GetCollect( _T( "Affix_Array_Direction" ) ) ) );
		rs->Close();
		
		AcGeVector3d vArrayDir;
		if ( AcGeExt::ConvertStringToVector( strAffixArrayDir, vArrayDir ) )
		{
			aJFormat->AffixArrayDirection( vArrayDir );
		}
		else
		{
			Real<AcGeVector3d> vRealArrayDir;
			aJFormat->AffixArrayDirection( vRealArrayDir );
		}

		m_JointFormats.Add( aJFormat );
		return aJFormat;
	}
	catch ( _com_error e )
	{
		ErrorAlert(e);
		if ( rs->GetState() == adOpenStatic )
			rs->Close();
		if ( aJFormat )
			delete aJFormat;
		return NULL;
	}
}
}